function R = affine_seq(imfun, xv, yv, X, ts)
%AFFINE_SEQ Image sequence from affine flow
%
%   R = AFFINE_SEQ(imfun, imsiz, X, ts);
%
%       Generates a sequence of image frames based on an affine flow.
%
%       Input arguments:
%       - imfun:        An image generating function.
%
%                       I = imfun(xcoords, ycoords);
%
%                       Given two maps of x and y coordinates, this
%                       function yields a value map I of the same size,
%                       which comprises the pixel values at specified
%                       locations.
%
%       - xv:           The vector of x-coordinates of grid points
%
%       - yv:           The vector of y-coordinates of grid points
%
%       - X:            The Lie algebraic matrix.
%
%       - ts:           The sequence of time steps at which the image
%                       frames are generated.
%
%       Output arguments:
%       - R:            The sequence of generated frames, of size
%                       [h, w, n], where n is the number of frames.
%

% Created by Dahua Lin, on April 4, 2012
%

%% verify input arguments

if ~isa(imfun, 'function_handle')
    error('affine_seq:invalidarg', ...
        'imfun should be a function handle.');
end

%% main

% generate sequence of transforms

n = numel(ts);
Ts = affine_flow(X, -ts);

% generate sequence of transformed images

[xx0, yy0] = meshgrid(xv, yv);
coords0 = [xx0(:).'; yy0(:).'];

imsiz = size(xx0);
R = zeros([imsiz, n]);

for i = 1 : n
    
    T = Ts(:,:,i);
    A = T(1:2, 1:2);
    b = T(1:2, 3);
    
    cs = bsxfun(@plus, A * coords0, b);
    xx = cs(1,:);
    yy = cs(2,:);
    
    xx = reshape(xx, imsiz);
    yy = reshape(yy, imsiz);
    
    I = imfun(xx, yy);
    R(:,:,i) = I;
    
end








